//总页数
let allPage = 1;
var today = new Date();
var todayTxt = today.format('yyyy-MM-dd');
$(document).ready(function () {
    loadItem();
    $(".my_date_control").datepicker({
        changeMonth: true,
        changeYear: true,
        dateFormat: 'yy-mm-dd',//日期格式
        yearSuffix: '年', //年的后缀
        showMonthAfterYear: true,//是否把月放在年的后面
        maxDate: (function () {
            return todayTxt;
        })(),//最大日期
        yearRange: (function () {
            // '1990:2010'
            let year1 = '1900';
            let year2 = today.getFullYear();
            return year1 + ':' + year2;
        })(),
        monthNamesShort: ['01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12'],
        dayNamesMin: ['日', '一', '二', '三', '四', '五', '六'],
        onUpdatedDatepicker: function (_year, _month, _calendar) {
            let $c = $('div#ui-datepicker-div');
            $c.find('td[data-handler="selectDay"]').each(function (idx, d) {
                let $d = $(d);
                $d.css({'pointer-events': 'auto', 'opacity': 1});
            });
        }
    });
});

$('#search').on('click', function () {
    loadItem();
});


function loadItem() {

    var params = getParams();
    if (params) {
        var $tbody = $('#tbodyViolateInstitutionList');
        $tbody.empty();
        WeKnow.loading();
        $.ajax({
            type: "POST",
            url: "violationRecord/loadPrescriptionViolateList",
            dataType: "json",
            data: params,
            async: true,//异步调用
            success: function (data) {
                WeKnow.closeLoading();
                if (!WeKnow.filterAjaxResult(data)) return false; // session过期check

                if (data.errcode == 0) {
                    let dataInfo = data.violationList;
                    if (dataInfo && dataInfo.length) {
                        let e = '';
                        $(dataInfo).each(function (idx, rowData) {
                            e += '<tr data-prescription_pic_url="' + rowData.prescriptionPicUrl + '" data-institution_id="' + rowData.institutionId + '"   >';
                            // 序号
                            e += '<td class="text-center">' + (idx + 1) + '</td>';
                            // 审核日期
                            e += '<td class="text-center">' + (rowData.auditTmStr ? rowData.auditTmStr : "-") + '</td>';
                            // 医疗机构
                            e += '<td class="text-center">' + (rowData.name ? rowData.name : "-") + '</td>';
                            // 违规类目
                            e += '<td class="text-center">' + (rowData.title ? rowData.title : "-") + '</td>';
                            // 违规内容
                            e += '<td class="text-center">' + (rowData.msgTxt ? rowData.msgTxt : "-") + '</td>';
                            // 处方图片
                            e += '<td class="text-center">';
                            e += '<a href="#"><span class="showImgSpan">查看</span></a>';
                            e += '</td>';
                            //审核人员
                            e += '<td class="text-center">' + (rowData.auditName ? rowData.auditName : "系统审核") + '</td>';
                            //上传人员
                            e += '<td class="text-center">' + (rowData.uploaderName ? rowData.uploaderName : "-") + '</td>';
                            //上传日期
                            e += '<td class="text-center">' + (rowData.uploadTmStr ? rowData.uploadTmStr : "-") + '</td>';
                            e += '</tr>';
                        });
                        $tbody.append(e);
                    } else {
                        WeKnow.notifyMsg('暂无数据');
                    }

                    //页码修改
                    if (allPage != data.pages) {
                        allPage = data.pages;
                        //重新生成页面下拉
                        let $selectPage = $('#selectPage');
                        $selectPage.empty();
                        let e = '';
                        for (let i = 1; i <= allPage; i++) {
                            e += '<option value="' + i + '">' + i + '</option>';
                        }
                        $selectPage.append(e);
                        //总页码
                        $(".allPage").html("共" + allPage + "页");
                    }
                    //限制上一页
                    if (params.pageNum == 1) {
                        $(".upPageDiv").html('上一页');
                    } else {
                        $(".upPageDiv").html('<a href="javascript:updatePage(' + (parseInt(params.pageNum) - 1) + ')">上一页</a>');
                    }
                    //限制下一页
                    if (params.pageNum >= allPage) {
                        $(".downPageDiv").html('下一页');
                    } else {
                        $(".downPageDiv").html('<a href="javascript:updatePage(' + (parseInt(params.pageNum) + 1) + ')">下一页</a>');
                    }
                    //修改当前页码
                    $("#selectPage").val(params.pageNum);
                } else if (data.errcode == -1) {
                    WeKnow.errorMsg(data.errMsg);
                } else {
                    WeKnow.systemMsg('查询失败，请重新登录后再试');
                }
            }
        });
    }
}


//查看图片
$("#tbodyViolateInstitutionList").on('click', '.showImgSpan', function (e) {
    //获取数据
    let $parentTr = $(this).closest('tr');
    MY_SHOW_IMG.showImg($parentTr.data('prescription_pic_url'), 0);
});


//页面变更
$("#selectPage").on('change', function (e) {
    updatePage($(this).val())
});
//limit变更
$("#pageSize").on('change', function (e) {
    //重置页码
    $("#pageNum").val(1);
    //查询数据
    loadItem();
});

//修改页码
function updatePage(pageNum) {
    //获取页码
    $("#pageNum").val(pageNum);
    //查询数据
    loadItem();
}


// 收集页面查询参数
function getParams() {

    var data = {};

    // 标本日期 from - to
    var $dtFrom = $('#dtFrom');
    var $dtTo = $('#dtTo');

    if (!$dtFrom.val().length) {
        WeKnow.errorMsg('请输入查询开始日期');
        $dtFrom.focus();
        return false;
    }

    var dtFrom = $dtFrom.val();
    if (!MY_VALIDATOR.isDate(dtFrom, 'yyyy-MM-dd')) {
        WeKnow.errorMsg('查询开始日期格式不正确');
        $dtFrom.focus();
        return false;
    }
    data.dtFrom = dtFrom;

    // 查询日期to

    if (!$dtTo.val().length) {
        WeKnow.errorMsg('请输入查询结束日期');
        $dtTo.focus();
        return false;
    }

    var dtTo = $dtTo.val();
    if (!MY_VALIDATOR.isDate(dtTo, 'yyyy-MM-dd')) {
        WeKnow.errorMsg('查询结束日期格式不正确');
        $dtTo.focus();
        return false;
    }
    if ($dtFrom.val() > $dtTo.val()) {
        WeKnow.errorMsg('开始时间不能大于结束时间');
        return false;
    }
    data.dtTo = dtTo;

    // 类型
    var type = $('#type').val();
    data.type = type;
    // 表名

    var institutionName = $('#institutionName').val();
    data.institutionName = institutionName;

    //页面
    data.pageNum = $("#pageNum").val();
    //一页数据加载数(limit)
    data.pageSize = $("#pageSize").val();

    return data;
}